<?php
/**
 * Controller_Admin_Eng
 *
 * Класс Controller_Admin_Eng отвечает за обработку списка слов
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Controller_Admin_Eng extends Controller_Admin {

  
	/**
	 * Выбор действия по URL
	 *
	 */
	public function action() {
		
		Service_Debug::setDebug();
		switch ($this->arrRequest[2]) {
			case '': case 'list':
				$fileId = !empty($this->arrRequest[3]) ? Service_Escape::absolutelyEscapeString($this->arrRequest[3]) : null;
		    $this->actionList($fileId);
				break;
			case 'parse':
				$this->actionParse();
				break;
			case 'delete_file':
				$idFile = intval($this->arrRequest[3]);
				if (empty($idFile)) throw new Exception_SiteError('', 404);
				$this->actionDeleteFile($idFile);
				break;
			case 'join_words_by_suffixes':
				$this->joinWordsBySuffixes();
				break;
			default:
				$strWord = Service_Escape::escapeString($this->arrRequest[2]);
				if (empty($strWord)) throw new Exception_SiteError('', 404);
				$objWord = new EngWord();
				$objWord->find('first', array('conditions' => "word = '" . $strWord ."'"));
				//$objDB = Service_Database::instance();
				//$stmt = "SELECT `ID_eng_word_1` FROM `eng_word_1` WHERE word = '" . $strWord ."'";
				//$wordId = $objDB->selectSimpleField($stmt);
				if (!$objWord->id)	throw new Exception_SiteError('', 404);
				$this->actionOne($objWord);
				break;
		}
	}

	/**
	 * Выводим все слова
	 *
	 */
	public function actionList($fileId = null) {
		$objDB = Service_Database::instance();
		$wordId = Service_Escape::requestDataInt('ID_eng_word_1', 'GET');
		$mark = Service_Escape::requestDataInt('mark', 'GET');
		if ($wordId && $mark) {
			$stmt = "UPDATE `eng_word_1` 
				SET `eng_word_1`.`mark` = '" . $mark . "'
				WHERE `eng_word_1`.`ID_eng_word_1` = '" . $wordId . "'
			";
			$objDB->query($stmt);
		}
		
		$stmt = "
			SELECT * FROM `eng_word_1`
			WHERE `eng_word_1_amount` >= 3
			ORDER BY `eng_word_1_amount` DESC
		";
		if ($fileId) {
			echo $fileId;
			$fileIdCondition = "`eng_file_word_1`.`ID_eng_file` = '" . $fileId . "'";
			if (strpos($fileId, ',')) $fileIdCondition =  "`eng_file_word_1`.`ID_eng_file` IN (" . $fileId . ")";
			$stmt = "
				SELECT `eng_file_word_1`.`amount` AS `file_amount`, `eng_word_1`.* FROM `eng_file_word_1`
				INNER JOIN `eng_word_1` USING(`ID_eng_word_1`)
				WHERE 
					" . $fileIdCondition . " AND
					(
						`eng_file_word_1`.`amount` >= 2 AND `eng_word_1_amount` >= 3
						OR `eng_word_1_amount` >=1
					)
				ORDER BY `eng_file_word_1`.`amount` DESC, `eng_word_1_amount` DESC
			";
		}
		$arrWords = $objDB->select($stmt);
    $this->title = $this->pageHead = 'Список слов';
    $this->showHeader();
    	echo '<table border>';
    	$i = 0;
    	$k = 0;
    	$amount = 0;
    	$totalAmount =0;
    	echo '<tr>';
    		echo '<th>id</th>';
    		echo '<th>k</th>';
    		echo '<th>i</th>';
    		echo '<th>word</th>';
    		if (!empty($arrWords[0]['file_amount']))  echo '<th>F amount</th>';
    		echo '<th>amount</th>';
    		echo '<th colspan="5"></th>';
    	echo '</tr>';
			foreach ($arrWords as $arrWord) {
				$k++;
				$totalAmount += $arrWord['eng_word_1_amount'];
				if ($arrWord['word_type'] || $arrWord['mark'] || strpos($arrWord['word'], "'")) {
					$amount += $arrWord['eng_word_1_amount'];
					continue;
				}
				echo '<tr ' . ($arrWord['word_type'] ? 'style="font-weight:bold"' : null) . '>';
					echo '<td>' . $arrWord['ID_eng_word_1'] . '</td>';
					echo '<td>' . ++$i . '</td>';
					echo '<td>' . ++$i . '</td>';
					echo '<td style="font-size:22px;"><a href="' . W_ADMIN . 'eng/' . $arrWord['word'] . '/">' . substr($arrWord['word'], 0 , 30) .  (strlen($arrWord['word']) > 30 ? '...' : null) . '</a></td>';
					if (!empty($arrWord['file_amount'])) echo '<td><b>' . $arrWord['file_amount'] . '</b></td>';
					echo '<td><b>' . $arrWord['eng_word_1_amount'] . '</b></td>';
					echo '<td><a href="' . W_ADMIN . 'eng/?ID_eng_word_1=' . $arrWord['ID_eng_word_1'] . '&mark=5">&nbsp; &nbsp;5&nbsp; &nbsp;</a></td>';
					echo '<td><a href="' . W_ADMIN . 'eng/?ID_eng_word_1=' . $arrWord['ID_eng_word_1'] . '&mark=4">&nbsp; &nbsp;4&nbsp; &nbsp;</a></td>';
					echo '<td><a href="' . W_ADMIN . 'eng/?ID_eng_word_1=' . $arrWord['ID_eng_word_1'] . '&mark=3">&nbsp; &nbsp;3&nbsp; &nbsp;</a></td>';
					echo '<td><a href="' . W_ADMIN . 'eng/?ID_eng_word_1=' . $arrWord['ID_eng_word_1'] . '&mark=2">&nbsp; &nbsp;2&nbsp; &nbsp;</a></td>';
					echo '<td><a href="' . W_ADMIN . 'eng/?ID_eng_word_1=' . $arrWord['ID_eng_word_1'] . '&mark=1">&nbsp; &nbsp;1&nbsp; &nbsp;</a></td>';
				echo '</tr>';
			}
			echo '</table>';
		echo '<br/>';
		echo '<br/>';
		echo '<br/>';
		echo $amount . ' / ' . $totalAmount;
		$this->showFooter();
	}
	

	/**
	 * Выводим конкретное слово
	 *
	 *@param EngWord - слово, с которым предстоит работа
	 */
	public function actionOne(EngWord $objWord) {
		$objDB = Service_Database::instance();
		$arrSenses = $objWord->getPopularSenses();
/*		$stmt = "
			SELECT * FROM `eng_word_1`
			ORDER BY `eng_word_1_amount` DESC
		";
		$arrWords = $objDB->select($stmt);
		*/
    $this->title = $this->pageHead = 'Список слов';
    $this->showHeader();
			echo '<h2>' . $objWord->getField('word') . '</h2>';
	    echo '<pre>';
	    	print_r($objWord->tableFields);
	    echo '</pre>';
	    foreach ($arrSenses as $arrSense) {
	    	echo '<p>' . $arrSense['eng_sense_text'] . '</p>';
	    }
		$this->showFooter();
	}
	
  
	/**
	 * Удаление слов определенного файла
	 * 
	 * @param ID $idFile
	 */
	public function actionDeleteFile($idFile) {
		Service_Debug::setDebug();
		$objEng = new Eng();
		$objEng->deleteFileWords();
		 
		$this->showHeader();
			echo 'Готово';
		
		$this->showFooter();
	}
	/**
	 * Парсинг
	 *
	 */
	public function actionParse() {
		Service_Debug::setDebug();
		
		$objEng = new EngWord();
		//$objEng->setStandardTypes();
		//$objEng->updateWordsWithQuptes();
		echo 1;
		$objEng = new Eng();
		$folderPath = "e:\\english\\ios_6\\";
		$objEng->readDirWithFiles($folderPath);
		exit();
		
		
		 
		$this->showHeader();
		
		
		$this->showFooter();
	}
	
	/**
	 * Объединяет слова друг с другом по суффиксам
	 */
	public function joinWordsBySuffixes() {
		Service_Debug::setDebug();
		$objEng = new EngWord();
		$objEng->joinWordsBySuffixes();
		echo '<br /><br />Готово';
	} 
}
?>